package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.LoginService;
import com.itheima.service.MemberService;
import com.itheima.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author hql
 * @date 2019-12-10 9:23
 */
@Service(interfaceClass = LoginService.class)
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private MemberService memberService;

    /**
     * 登录的验证，如果没有登录实现自动注册
     * @param telephone 电话号码
     * @param validateCode 传的验证码
     * @return
     */
    @Override
    public Result check(String telephone, String validateCode) {
        //判断验证码的信息是否正确
        if (!validateCodeService.checkValidateCode(telephone,validateCode, RedisMessageConstant.SENDTYPE_LOGIN)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //如果验证码正确则查看会员表中有没有该会员信息
        Member member=memberService.checkTelphone(telephone);
        //如果没有则直接注册会员信息
        if (member==null){
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.addMember(member);
        }
        return new Result(true,MessageConstant.LOGIN_SUCCESS,member);
    }
}
